package unq.tpi.persistencia.performanceEj.daos;

import java.util.List;

import org.hibernate.classic.Session;

import unq.tpi.persistencia.performanceEj.model.Employee;
import unq.tpi.persistencia.performanceEj.model.EmployeeResume;
import unq.tpi.persistencia.util.SessionManager;

public class EmployeeDAO {

	public Employee getByName(final String name, final String lastName) {
		Session session = SessionManager.getSession();
		return (Employee) session
				.createQuery(
						"from Employee where firstName = :name and lastName = :lastName")
				.setParameter("name", name).setParameter("lastName", lastName)
				.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<Employee> getAll() {
		Session session = SessionManager.getSession();
		return session.createCriteria(Employee.class).list();
	}

	public Employee getByCode(int id) {
		Session session = SessionManager.getSession();
		return (Employee) session.load(Employee.class, id);
	}

	public List<EmployeeResume> getAllOrderedBySalary() {
		Session session = SessionManager.getSession();
		return (List<EmployeeResume>) session
				.createQuery(
						"select new unq.tpi.persistencia.performanceEj.model.EmployeeResume(e.firstName, e.lastName, s.amount) FROM Employee as e inner join e.salaries as s  where s.to = '9999-01-01' ORDER BY s.amount DESC")
				.setMaxResults(10)
				.list();
		
	}

}
